﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для ucFuelType.xaml
    /// </summary>
    public partial class ucFuelType : UserControl
    {

        private routelistEntities db;
        private fuel_type item;
        private int _id;



        public CollectionViewSource ItemsSource
        {
            get { return (CollectionViewSource)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(CollectionViewSource), typeof(ucFuelType), new UIPropertyMetadata(null, new PropertyChangedCallback(propItemsSource)));

        private static void propItemsSource(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ucFuelType item = d as ucFuelType;
            if (null == item) return;
        }


        public fuel_type FuelType
        {
            get { return (fuel_type)GetValue(FuelTypeProperty); }
            set { SetValue(FuelTypeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FuelType.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FuelTypeProperty =
            DependencyProperty.Register("FuelType", typeof(fuel_type), typeof(ucFuelType), new UIPropertyMetadata(null, new PropertyChangedCallback(propFuelTypeChanged)));

        private static void propFuelTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ucFuelType item = (ucFuelType)d;
            if (null == item) return;
            item.item = (fuel_type)e.NewValue;
            item.LoadFuelData(item.item);
        }


        public int FuelTypeID
        {
            get { return (int)GetValue(FuelTypePropertyID); }
            set { SetValue(FuelTypePropertyID, value); }
        }

        // Using a DependencyProperty as the backing store for FuelType.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FuelTypePropertyID =
            DependencyProperty.Register("FuelTypeID", typeof(int), typeof(ucFuelType), new UIPropertyMetadata(0, new PropertyChangedCallback(propchanged)));

        
        public ucFuelType()
        {
            InitializeComponent();
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                db = new routelistEntities(Properties.Settings.Default.DebugConnection);
            }
            else
            {
                db = new routelistEntities();
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //if (FuelType != null)
            //{
            //    LoadFuelData(FuelType);
            //    return;
            //}
            //LoadFuelTypeDataID(FuelTypeID);
        }

        private static void propchanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ucFuelType sh = (ucFuelType)d;
            sh._id = (int)e.NewValue;
            sh.LoadFuelTypeDataID(sh._id);
        }

        private void LoadFuelTypeDataID(int id)
        {
            //if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) return;
            if (ItemsSource == null)
            {
                item = (from o in db.fuel_type where o.id == id select o).FirstOrDefault();
            }
            else
            {
                item = ItemsSource.View.CurrentItem as fuel_type;
            }
            if (null != item)
            {
                textBlock1.Text = item.name;
            }
        }

        private void LoadFuelData(fuel_type id)
        {
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) return;
            item = id;
            if (null != item)
            {
                textBlock1.Text = item.name;
            }
        }

        private void buttonSelect_Click(object sender, RoutedEventArgs e)
        {
            wFuelType wnd;
            wnd = new wFuelType();
            if (null != ItemsSource)
            {
                wnd.ItemsSource = ItemsSource;
                
            }
            else
            {
                if (null != item)
                    wnd.FuelTypeId = item.id;
            }
            if (true == wnd.ShowDialog())
            {
                if (null != ItemsSource)
                {
                    item = wnd.FuelType;
                    FuelType = item;
                    LoadFuelData(item);
                }
                else
                {
                    _id = wnd.FuelTypeId;
                    FuelTypeID = _id;
                    LoadFuelTypeDataID(_id);
                }
            }
        }
    }
}
